<template>
  <ele-modal
    form
    :width="840"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <!-- 表格 -->
    <ele-pro-table
      ref="tableRef"
      row-key="roleId"
      :columns="columns"
      :datasource="datasource"
      :show-overflow-tooltip="true"
      v-model:selections="selections"
      highlight-current-row
      cache-key="systemRoleTable"
      :pagination="false"
    >
      <template #toolbar>
        <el-button
          type="success"
          class="ele-btn-icon"
          :icon="Plus"
          @click="openEdit()"
        >
          新建
        </el-button>
      </template>
      <template #action="{ row }">
        <el-link type="danger" :underline="false" @click="remove(row)">
          删除
        </el-link>
      </template>
    </ele-pro-table>

    <!-- 编辑弹窗 -->
    <edit
      v-model="showEdit"
      :data="current"
      :student-id="studentId"
      @done="reload"
    />
  </ele-modal>
</template>

<script setup>
  import { ref, watch } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import { ElMessageBox } from 'element-plus/es';
  import { EleMessage } from 'ele-admin-plus/es';
  import Edit from './components/edit.vue';
  import { pageMemberTax, removeMemberTax } from '@/api/company';

  const emit = defineEmits(['done', 'update:modelValue']);
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object,
    studentId: Number
  });

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'belongsMonth',
      label: '所属月份'
    },
    {
      prop: 'amount',
      label: '金额',
      width: 150
    },
    {
      prop: 'create_time',
      label: '录入时间',
      width: 150
    },
    {
      prop: 'create_user',
      label: '录入人',
      width: 150
    },

    {
      columnKey: 'action',
      label: '操作',
      width: 80,
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const title = ref('');

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, where, orders }) => {
    return pageMemberTax({
      ...where,
      ...orders,
      page,
      limit: 999999999,
      member_id: props.studentId
    });
  };

  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({
      page: 1,
      where
    });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 删除单个 */
  const remove = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要删除“' +
        rows.map((d) => d.belongsMonth).join(', ') +
        '”的个税记录吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        removeMemberTax({ id })
          .then((msg) => {
            loading.close();
            EleMessage.success(msg);
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };
  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        if (props.data) {
          title.value = props.data.realname + '的个税信息';
          reload();
        }
      } else {
        console.log('什么也没有');
      }
    }
  );
</script>

<script>
  export default {
    name: 'SystemRole'
  };
</script>
